package de.global42.clubapp.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
/**
 * @author lutz
 *
 */
@Entity
public class Mission implements Serializable{

   /**
	 * Generated Version UID
	 */
	private static final long serialVersionUID = -3551724154928235405L;

@Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", updatable = false, nullable = false)
   private Long id = null;
   
   @Version
   @Column(name = "version")
   private int version = 0;
   
   @Column
   @NotNull
   protected String name;
   
   @Temporal(TemporalType.TIMESTAMP)
   @NotNull
   protected Date startDate;
   
   @Temporal(TemporalType.TIMESTAMP)
   protected Date endDate;
   
   @Column
   @Lob
   protected String description;
   
   @ManyToMany(mappedBy = "missions")
   protected Set<Venue> venues = new HashSet<Venue>();
   
   @ManyToOne(cascade = CascadeType.DETACH, optional = true)
   protected Coupon coupon;
   
   @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn
   protected MissionTarget target;
   
   @OneToMany(mappedBy = "mission", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   protected Set<ValidationCode> validationCodes;
   
   public Long getId()
   {
      return this.id;
   }

   public void setId(final Long id)
   {
      this.id = id;
   }

   public int getVersion()
   {
      return this.version;
   }

   public void setVersion(final int version)
   {
      this.version = version;
   }

   public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public Date getStartDate() {
	return startDate;
}

public void setStartDate(Date startDate) {
	this.startDate = startDate;
}

public Date getEndDate() {
	return endDate;
}

public void setEndDate(Date endDate) {
	this.endDate = endDate;
}

public String getDescription() {
	return description;
}

public void setDescription(String description) {
	this.description = description;
}

public Set<Venue> getVenues() {
	return venues;
}

public void setVenues(Set<Venue> venues) {
	this.venues = venues;
}

public Coupon getCoupon() {
	return coupon;
}

public void setCoupon(Coupon coupon) {
	this.coupon = coupon;
}

public MissionTarget getTarget() {
	return target;
}

public void setTarget(MissionTarget target) {
	this.target = target;
}

public Set<ValidationCode> getValidationCodes() {
	return validationCodes;
}

public void setValidationCodes(Set<ValidationCode> validationCodes) {
	this.validationCodes = validationCodes;
}

public String toString()
   {
      String result = "";
      if (id != null)
         result += id;
      return result;
   }

   @Override
   public boolean equals(Object that)
   {
      if (this == that)
      {
         return true;
      }
      if (that == null)
      {
         return false;
      }
      if (getClass() != that.getClass())
      {
         return false;
      }
      if (id != null)
      {
         return id.equals(((Mission) that).id);
      }
      return super.equals(that);
   }

   @Override
   public int hashCode()
   {
      if (id != null)
      {
         return id.hashCode();
      }
      return super.hashCode();
   }

	public boolean issuedValidationCode(ValidationCode codeToBeVerified) {
		return validationCodes.contains(codeToBeVerified);
	}
}